package com.hp.tiger.chapter15;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author XavierDong
 **/
public class MessageHandler {

    private static final Random RANDOM = new Random(System.currentTimeMillis());

    private static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(5);

    public void request(Message message) {
        EXECUTOR.execute(() -> {
            String value = message.getValue();
            try {
                TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(1000));
                System.out.println("The message will be handle by " + Thread.currentThread().getName() + value);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    public void shutdown() {
        EXECUTOR.shutdown();
    }

}
